<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');
$fine   = 0;
$amount = 0;

//////////
$party       = array();

$selectParty = "SELECT partyId,partyName 
                  FROM party
                 WHERE partyId = ".$_POST['partyId']."
                 ORDER BY partyName";
$selectPartyRes = mysql_query($selectParty);
$a = 0;
while($partyRow = mysql_fetch_assoc($selectPartyRes))
{
	$party[$a]['id']     = $partyRow['partyId'];
	$party[$a]['name']   = $partyRow['partyName'];
	$party[$a]['fine']   = 0;
	$party[$a]['amount'] = 0;
	
	
  $selectVeepariBook = "SELECT vepaaribilldetail.fine,vepaaribilldetail.amount,vepaaribilldetail.CrDr,vepaaribillmaster.PartyId
	                        FROM vepaaribilldetail
	                        JOIN vepaaribillmaster ON vepaaribilldetail.vepaariBookMasterId = vepaaribillmaster.vepaariBookMasterId 
	                       WHERE vepaaribillmaster.partyId = ".$partyRow['partyId']."
                           AND vepaariBookDate <= '".date("Y-m-d")."'
	                       ORDER BY vepaariBookDate, vepaariBookDetailId";
	$selectVeepariBookRes = mysql_query($selectVeepariBook);
	while($row = mysql_fetch_assoc($selectVeepariBookRes))
	{
		if($row['CrDr'] == "Dr")
		{
			$party[$a]['fine']   += $row['fine'];
	    $party[$a]['amount'] += $row['amount'];
		}
		else
		{
			$party[$a]['fine']   -= $row['fine'];
	    $party[$a]['amount'] -= $row['amount'];
		}
	}
	
	$billBook = "SELECT totalFine,totalAmount FROM billbook
	              WHERE partyId = ".$partyRow['partyId']."
	              ORDER BY billBookId";
	$billBookRes = mysql_query($billBook);
	while($billBook = mysql_fetch_assoc($billBookRes))
	{
		$party[$a]['fine']   += $billBook['totalFine'];
    $party[$a]['amount'] += $billBook['totalAmount'];
	}
	
	$fine   += $party[$a]['fine'];
	$amount += $party[$a]['amount'];
	$a++;
}

echo number_format($fine, 0, '.', '').",".number_format($amount, 0, '.', '');
//echo "<b>Prev. Fine</b> <input type='text' name='partyFine' id='partyFine' value='".number_format($fine, 2, '.', '')."'/> ";
//echo "<b>Prev. Amount</b> <input type='text' name='partyAmount' id='partyAmount' value='".number_format($amount, 2, '.', '')."'/>";
?>